package ch09.person.dao.mybatis;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import ch09.MyBatisFactory;
import ch09.person.domain.Person;

public class PersonDao {

	/* 사람 한명 조회 */
	public Person selectPerson(int id) {
		SqlSession session = MyBatisFactory.getSession();
		Person person = session.selectOne("p.selectPerson", id);
		session.close();
		return person;
	}

	/* 사람 여러명 조회 */
	public List<Person> selectAllPerson() {
		SqlSession session = MyBatisFactory.getSession();
		List<Person> list = session.selectList("p.selectAllPerson");
		session.close();
		return list;
	}

	/* 사람 한명 입력 */
	public int insertPerson(Person person) {
		SqlSession session = MyBatisFactory.getSession();
		int count = session.insert("p.insertPerson", person);
		session.commit();
		session.close();
		return count;
	}

	public void updatePerson(Person person) {
		SqlSession session = MyBatisFactory.getSession();
		session.update("p.updatePerson", person);
		session.commit();
		session.close();
	}

	/* 사람 한명 삭제 */
	public void deletePerson(int id) {
		SqlSession session = MyBatisFactory.getSession();
		session.delete("p.deletePerson", id);
		session.commit();
		session.close();
	}
}
